package com.liao;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 生产数据
 */
public class Producer {
    private String start ="2019-01-01";
    private String end = "2020-01-01";

    ArrayList<String> phoneNum = new ArrayList<String>();

    Map<String,String> phoneName = new HashMap<String, String>();

    public void init(){
        phoneNum.add("13520903125");
        phoneNum.add("16910901085");
        phoneNum.add("13448821297");
        phoneNum.add("15889335163");
        phoneNum.add("17412022282");
        phoneNum.add("17896343723");
        phoneNum.add("14266953292");
        phoneNum.add("15215126494");
        phoneNum.add("15203199379");
        phoneNum.add("15759416288");
        phoneNum.add("19263131999");
        phoneNum.add("16327349720");
        phoneNum.add("13186500722");
        phoneNum.add("15587479443");
        phoneNum.add("14524183839");
        phoneNum.add("18438995153");
        phoneNum.add("18479781152");
        phoneNum.add("14365454746");
        phoneNum.add("19126668865");
        phoneNum.add("19989730877");
        phoneNum.add("16903221309");
        phoneNum.add("19067315772");
        phoneNum.add("18745938848");
        phoneNum.add("16896692875");
        phoneNum.add("13849032256");
        phoneNum.add("18578481115");
        phoneNum.add("15806088370");
        phoneNum.add("18311269351");
        phoneNum.add("15225974702");
        phoneNum.add("19554963714");
        phoneNum.add("13851365640");
        phoneNum.add("14306688270");
        phoneNum.add("16341910532");
        phoneNum.add("14203798814");
        phoneNum.add("17180970975");
        phoneNum.add("14518469456");
        phoneNum.add("19129261588");
        phoneNum.add("14974457759");
        phoneNum.add("19275609227");
        phoneNum.add("15688177854");
        phoneNum.add("14998094597");
        phoneNum.add("14310022062");
        phoneNum.add("13084986182");
        phoneNum.add("13702638948");
        phoneNum.add("19149893646");
        phoneNum.add("16750253910");
        phoneNum.add("15292418104");
        phoneNum.add("14601963538");
        phoneNum.add("17943674414");
        phoneNum.add("15654757543");
        phoneNum.add("14852574413");
        phoneNum.add("14622059807");
        phoneNum.add("16703327538");
        phoneNum.add("17503731439");
        phoneNum.add("15583578253");
        phoneNum.add("15075386058");
        phoneNum.add("15850070766");
        phoneNum.add("18091697630");
        phoneNum.add("16719852599");

        phoneName.put("13520903125","周伟业");
        phoneName.put("16910901085","林子明");
        phoneName.put("13448821297","庄静丽");
        phoneName.put("15889335163","陈小婷");
        phoneName.put("17412022282","张杨平");
        phoneName.put("17896343723","武冠轩");
        phoneName.put("14266953292","周泉江");
        phoneName.put("15215126494","余炜鹏");
        phoneName.put("15203199379","张灿云");
        phoneName.put("15759416288","许伟谊");
        phoneName.put("19263131999","郭锦贤");
        phoneName.put("16327349720","朱泽亮");
        phoneName.put("13186500722","吴羽静");
        phoneName.put("15587479443","张铉");
        phoneName.put("14524183839","蔡迪全");
        phoneName.put("18438995153","林炎秀");
        phoneName.put("18479781152","梁梦怡");
        phoneName.put("14365454746","刁慧彬");
        phoneName.put("19126668865","陈增宏");
        phoneName.put("19989730877","刘瑞雄");
        phoneName.put("16903221309","刘俊声");
        phoneName.put("19067315772","陈学斌");
        phoneName.put("18745938848","王纯淇");
        phoneName.put("16896692875","梁绍荣");
        phoneName.put("13849032256","梁成鑫");
        phoneName.put("18578481115","廖庭康");
        phoneName.put("15806088370","张华扬");
        phoneName.put("18311269351","熊欢");
        phoneName.put("15225974702","马晓华");
        phoneName.put("19554963714","梁夏天");
        phoneName.put("13851365640","叶学斌");
        phoneName.put("14306688270","麦亮森");
        phoneName.put("16341910532","陈文创");
        phoneName.put("14203798814","苏日锐");
        phoneName.put("17180970975","庄海富");
        phoneName.put("14518469456","黄宇康");
        phoneName.put("19129261588","张铭");
        phoneName.put("14974457759","杨晴晴");
        phoneName.put("19275609227","姚能燕");
        phoneName.put("15688177854","陈培凤");
        phoneName.put("14998094597","杜国仲");
        phoneName.put("14310022062","黄俊轩");
        phoneName.put("13084986182","邓伟键");
        phoneName.put("13702638948","叶钦帅");
        phoneName.put("19149893646","何俊霖");
        phoneName.put("16750253910","文嘉星");
        phoneName.put("15292418104","王程锋");
        phoneName.put("14601963538","邹子庆");
        phoneName.put("17943674414","邓校君");
        phoneName.put("15654757543","杨君胜");
        phoneName.put("14852574413","陈煜");
        phoneName.put("14622059807","罗锦坚");
        phoneName.put("16703327538","黄锦晖");
        phoneName.put("17503731439","吴文悦");
        phoneName.put("15583578253","李伟豪");
        phoneName.put("15075386058","李亮蓉");
        phoneName.put("15850070766","邱泽松");
        phoneName.put("18091697630","杨建民");
        phoneName.put("16719852599","杨泳志");
    };
    public String productLog() throws ParseException {
        String caller;
        String callee;
        String buildTime;
        int dura;

        //1.随机选择两个不同的电话号码
        int callerIndex = (int)(Math.random()*phoneNum.size()) ;
        caller = phoneNum.get(callerIndex);

        while (true){
            int calleeIndex = (int)(Math.random()*phoneNum.size()) ;
            callee = phoneNum.get(calleeIndex);
            if (callerIndex!=calleeIndex) break;
        }
        //2.随机生成通话建立时间（start，stop）

        buildTime = randomBuildTime(start,end);
        //3.随机生成通话时长

        dura = (int)(Math.random() * 30 * 60)+1;
        //将数字转换成四位，不够四位则补0，如12转为0012
        DecimalFormat decimalFormat = new DecimalFormat("0000");
        String format = decimalFormat.format(dura);
        return caller+","+callee+","+buildTime+","+dura+"\n";
    }
    public String randomBuildTime(String start,String end) throws ParseException {
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        long startPiont = simpleDateFormat1.parse(start).getTime();
        long endPoint = simpleDateFormat1.parse(end).getTime();
        long resultTS = startPiont + (long) (Math.random() * (endPoint - startPiont));
        return simpleDateFormat2.format(new  Date(resultTS));
    };
    public void wirteLog(String path) throws IOException, ParseException, InterruptedException {

        FileOutputStream fos = new FileOutputStream(path);
        OutputStreamWriter osw = new OutputStreamWriter(fos);

        while (true){
            String log = productLog();
            System.out.print(log);
            osw.write(log);
            osw.flush();
            Thread.sleep(300);
        }
    }

    public static void main(String[] args) throws ParseException, InterruptedException, IOException {
        if (args.length<=0){
            System.out.println("没有参数");
            System.exit(0);
        }
        Producer producer = new Producer();
        producer.init();
        producer.wirteLog(args[0]);
    }
}
